package com.example.webgobang.service.impl;

import com.example.webgobang.mapper.UserMapper;
import com.example.webgobang.common.Constants;
import com.example.webgobang.model.domin.User;
import com.example.webgobang.service.UserService;
import com.example.webgobang.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.example.webgobang.common.Constants.*;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User userRegister(String username, String password, String checkPassword, HttpServletRequest request) {
        // 1.校验输入
        if(StringUtils.isAnyBlank(username, password)) { //有任意空白
            return null;
        }
        if(username.length() < ACCOUNT_LEAST_LENGTH) {
            return null;
        }
        if(password.length() < PASSWORD_LEAST_LENGTH
                || checkPassword.length() < PASSWORD_LEAST_LENGTH) {
            return null;
        }
        if(!password.equals(checkPassword)) {
            return null;
        }
        String charDict = "!@#$%^&*()_-+=<>?/{}[]|";
        for(Character c : username.toCharArray()) {
            if(charDict.contains(c.toString())) {
                return null;
            }
        }
        if(userMapper.selectByName(username) != null) {
            return null;
        }

        // 2.加密密码
        String securityPassword = SecurityUtils.encrypt(password);

        // 3.插入数据
        User user = new User(); //用于插入数据库的user，信息不全，没有设置的信息为数据库缺省值
        user.setUsername(username);
        user.setPassword(securityPassword);
        userMapper.insertUser(user);

        // 4.查询用户，用户信息脱敏
        User dbUser = userMapper.selectByName(username); //从数据库查出的user信息是全的
        user.setPassword("");

        // 5.记录用户登录态
        request.getSession(true).setAttribute(SESSION_USER_KEY, dbUser);

        return dbUser;
    }

    @Override
    public User userLogin(String username, String password, HttpServletRequest request) {
        // 1.校验输入
        if(StringUtils.isAnyBlank(username, password)) { //有任意空白
            return null;
        }
        if(username.length() < ACCOUNT_LEAST_LENGTH) {
            return null;
        }
        if(password.length() < PASSWORD_LEAST_LENGTH) {
            return null;
        }
        String charDict = "!@#$%^&*()_-+=<>?/{}[]|";
        for(Character c : username.toCharArray()) {
            if(charDict.contains(c.toString())) {
                return null;
            }
        }

        // 2.查询用户
        User user = userMapper.selectByName(username);
        if(user == null) {
            log.info("用户名或密码错误 code: 1"); //用户不存在
            return null;
        }
        if(!SecurityUtils.verify(password, user.getPassword())) {
            log.info("用户名或密码错误 code: 2"); //密码错误
            return null;
        }

        // 3.用户信息脱敏
        user.setPassword("");

        // 4.记录用户登录态
        request.getSession(true).setAttribute(SESSION_USER_KEY, user);

        return user;
    }

    @Override
    public User getUserById(Integer userId) {
        User user = null;
        try {
            user = userMapper.selectById(userId);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return user;
    }
}
